"""
@Author  : 康帅
@Time    : 2021/6/29 14:58
@Function: 在函数中嵌入装饰器
"""

from functools import wraps


def login(logfile='out.log'):
    """
    将日志文件存储到文件夹中
    以带参数装饰器去实现
    :param logfile:
    :return:
    """
    def logging_decorator(func):
        @wraps(func)
        def wrapper_function(*args, **kwargs):
            log_string = func.__name__ + "was called"
            print(log_string)
            with open(logfile, 'a') as opened_file:
                opened_file.write(log_string + '\n')
            return func(*args, **kwargs)
        return wrapper_function
    return logging_decorator


@login()
def my_func1():
    pass


@login(logfile='func2.log')
def my_func2():
    pass


if __name__ == '__main__':
    # 输出: my_func1 was called
    # 现在一个叫做 out.log 的文件出现了，里面的内容就是上面的字符串
    my_func1()

    # 输出: my_func2 was called
    # 现在一个叫做 func2.log 的文件出现了，里面的内容就是上面的字符串
    my_func2()
